package cn.stronglink.assetmanage.business.inventory.view.count;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.inventory.adapter.InventoryListAdapter;
import cn.stronglink.assetmanage.business.inventory.adapter.InventoryOrderAdapter;
import cn.stronglink.assetmanage.business.inventory.view.IInventoryView;
import cn.stronglink.assetmanage.business.inventory.view.detail.InventoryDetailFragment;
import cn.stronglink.assetmanage.entity.Inventory;
import cn.stronglink.assetmanage.entity.InventoryDetail;
import cn.stronglink.assetmanage.utils.Constants;

/**
 * Created by chencongcong on 2018/6/15.
 */

public class InventoryCountView implements IInventoryView{
    private Fragment fragment;
    //已盘点资产列表布局
    @BindView(R.id.ll_inventorycount_order_name)
    LinearLayout ll_inventorycount_order_name;
    //表单
    @BindView(R.id.tv_inventorycount_order_name)
    TextView tv_inventorycount_order_name;
    //操作日期
    @BindView(R.id.tv_inventorycount_operation_date)
    TextView tv_inventorycount_operation_date;
    //已盘点表单列表
    @BindView(R.id.rv_inventorycount_order_list)
    RecyclerView rv_inventorycount_order_list;
    //每个表单中的资产列表
    @BindView(R.id.rv_inventorycount_list)
    ExpandableListView rv_inventorycount_list;

    //表单列表adapter
    private InventoryListAdapter listAdapter;
    //资产列表adapter
    private InventoryOrderAdapter orderAdapter;
    public InventoryCountView(Fragment fragment, View view, InventoryListAdapter listAdapter, InventoryOrderAdapter orderAdapter) {
        this.fragment = fragment;
        ButterKnife.bind(this,view);
        this.listAdapter = listAdapter;
        this.orderAdapter = orderAdapter;
    }

    public Fragment getFragment() {
        return fragment;
    }

    /**
     * 显示已盘点列表
     */
    public void showListView(){
        rv_inventorycount_order_list.setVisibility(View.VISIBLE);
        ll_inventorycount_order_name.setVisibility(View.GONE);
        rv_inventorycount_list.setVisibility(View.GONE);
    }

    /**
     * 显示已盘点资产列表
     */
    public void showAssetView(){
        rv_inventorycount_order_list.setVisibility(View.GONE);
        ll_inventorycount_order_name.setVisibility(View.VISIBLE);
        rv_inventorycount_list.setVisibility(View.VISIBLE);
    }

    /**
     *设置已盘点表单数据
     * @param inventoryList
     */
    public void setListData(List<Inventory> inventoryList){
        listAdapter.notifyDatasChanged(inventoryList);
    }

    /**
     * 设置已盘点资产数据
     */
    public void setAssetData(Map<String,List<InventoryDetail>> inventoryDetailList){
        orderAdapter.setItemNameList(inventoryDetailList);
        orderAdapter.notifyDataSetChanged();
    }

    @Override
    public void showAssetTitle(Inventory inventory) {
        tv_inventorycount_order_name.setText(inventory.getId_());
        tv_inventorycount_operation_date.setText(inventory.getUpdateTime());
    }

    @Override
    public void showOrderDetail(InventoryDetail detail) {
        //跳转到详情页面
        Fragment detailFragment = new InventoryDetailFragment();
        FragmentManager fragmentManager = fragment.getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.frame_business,detailFragment);
        transaction.commit();
        Bundle bundle=new Bundle();
        bundle.putString(Constants.INVENTORY_DETAIL_NO, detail.getAssetNo());
        detailFragment.setArguments(bundle);
    }
}
